<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Books;
use App\Models\BookShelf;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class BookShelfController extends Controller{


    /**
     * 书架列表
     * */
    public function bookShelfList()
    {
        $userId = Auth::id();

        $bookShelfList = BookShelf::getBookShelfList($userId);

        return jsonSuccessData(['bookList'=>$bookShelfList]);

    }

    /**
     * 加入书架
     * */
    public function addBookShelf()
    {
        $bookId = Input::get('bookId');
        if ($bookId == 0){
            return jsonErrorData('INVALID_PARAM');
        }
        $index = Input::get('index',1);
        $lastReadChapter = Input::get('lastReadChapter',0);

        $userId = Auth::id();

        /*获取用户加入书架的总数*/
        $userBookShelfCount = BookShelf::getBookShelfCount($userId);
        if ($userBookShelfCount >= BookShelf::$bookShelfLimit){
            return echoJson('书架最多加入'.BookShelf::$bookShelfLimit.'本小说~',1008);
        }
        /*获取用户指定书架详情*/
        $bookShelf = BookShelf::getBookShelfInfo($userId,$bookId);

        /*若存在则修改书架详情，反之新增书架*/
        if (!empty($bookShelf)){
            $update = [];
            $update['status'] = BookShelf::SUCCESS_STATUS;
            if ($lastReadChapter != 0){
                $update['last_read_chapter'] = $lastReadChapter;
            }
            if ($index != 1){
                $update['index'] = $index;
            }
            BookShelf::where('id',$bookShelf['id'])->update($update);
        }else{
            //新增
            //获取第一章节的ID
            if ($lastReadChapter == 0){
                $lastReadChapter = Books::getFirstChapterIdById($bookId);
            }

            $bookShelf = new BookShelf();
            $bookShelf->book_id = $bookId;
            $bookShelf->user_id = $userId;
            $bookShelf->last_read_chapter = $lastReadChapter;
            $bookShelf->index = $index;
            $bookShelf->save();
        }

        return echoJson('加入书架成功',200);
    }

    /**
     * 移出书架
     * */
    public function delBookShelf()
    {
        $bookShelfId = Input::get('bookShelfId');
        if (empty($bookShelfId)){
            return jsonErrorData('INVALID_PARAM');
        }

        $bookShelfIdArr = explode(',',$bookShelfId);

        $userId = Auth::id();

        $result = BookShelf::where('user_id',$userId)->whereIn('id',$bookShelfIdArr)->update(['status'=>BookShelf::ERROR_STATUS]);

        if ($result){
            return echoJson('移除书架成功',200);
        }else{
            return echoJson('移除书架失败',500);
        }

    }


    /**
     * 修改书架最近阅读章节
     * */
    public function saveBookShelf()
    {
        $bookId = Input::get('bookId');
        if ($bookId == 0){
            return jsonErrorData('INVALID_PARAM');
        }
        $index = Input::get('index',1);

        $lastReadChapter = Input::get('lastReadChapter',0);
        if ($lastReadChapter == 0){
            $lastReadChapter = Books::getFirstChapterIdById($bookId);
        }

        $userId = Auth::id();

        $bookShelf = BookShelf::where('user_id',$userId)
            ->where('book_id',$bookId)
            ->where('status',BookShelf::SUCCESS_STATUS)
            ->first();
        if (empty($bookShelf)){
            return echoJson('该小说不存在',1008);
        }

        $result = $bookShelf->update([
            'last_read_chapter'=> $lastReadChapter,
            'index' => $index,
        ]);
        if ($result){
            return echoJson('修改书架成功',200);
        }else{
            return echoJson('修改书架失败',500);
        }

    }


}
